package com.wcl.hencodertest.test07_drawing;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.wcl.hencodertest.Util;

/**
 * @项目名： HenCoderTest
 * @包名： com.wcl.hencodertest.test07_drawing
 * @文件名: CameraView
 * @创建者: kilin
 * @创建时间: 2020/4/18 14:18
 * @描述： TODO
 */
public class CameraView extends View {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public static final int BITMAP_WIDTH = ConvertUtils.dp2px(250);

    public static final int MARGIN = ConvertUtils.dp2px(50);

    private Camera camera = new Camera();


    private float mValue;
    private float angle;
    private ValueAnimator mValueAnimator;

    public CameraView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    {
        camera.rotateX(45);
        camera.setLocation(0, 0, getZForCamera());
        mValueAnimator = ValueAnimator.ofFloat(0, 1);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mValue = (float) valueAnimator.getAnimatedValue();
                angle = 360 * mValue;
                invalidate();
            }
        });
        mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimator.setRepeatMode(ValueAnimator.RESTART);
        mValueAnimator.setDuration(1000);
        mValueAnimator.start();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        整张图变形
//        canvas.translate(MARGIN + BITMAP_WIDTH / 2F, MARGIN + BITMAP_WIDTH / 2F);
//        camera.applyToCanvas(canvas);
//        canvas.translate(-(MARGIN + BITMAP_WIDTH / 2F), -(MARGIN + BITMAP_WIDTH / 2F));
//        canvas.drawBitmap(Util.getAvatar(BITMAP_WIDTH), MARGIN, MARGIN, paint);

        canvas.save();
        canvas.translate(MARGIN + BITMAP_WIDTH / 2F, MARGIN + BITMAP_WIDTH / 2F);//6.将第2步的平移倒回来
        canvas.rotate(-angle);//5. 将第三部的旋转倒回来
        canvas.clipRect(-BITMAP_WIDTH, -BITMAP_WIDTH, BITMAP_WIDTH, 0);//4.截取画布
        canvas.rotate(angle);//3.旋转一定角度
        canvas.translate(-(MARGIN + BITMAP_WIDTH / 2F), -(MARGIN + BITMAP_WIDTH / 2F));//2.图片中心挪到0,0点
        canvas.drawBitmap(Util.getAvatar(BITMAP_WIDTH), MARGIN, MARGIN, paint);//1.先把图片画出来,注意坐标
        canvas.restore();

        canvas.save();
        canvas.translate(MARGIN + BITMAP_WIDTH / 2F, MARGIN + BITMAP_WIDTH / 2F);//7.将第二部的平移倒回来
        canvas.rotate(-angle);//6. 将第三部的旋转倒回来
        camera.applyToCanvas(canvas);//5.camera旋转
        canvas.clipRect(-BITMAP_WIDTH, 0, BITMAP_WIDTH, BITMAP_WIDTH);//4.截取画布
        canvas.rotate(angle);//3.旋转一定角度
        canvas.translate(-(MARGIN + BITMAP_WIDTH / 2F), -(MARGIN + BITMAP_WIDTH / 2F));//2.图片中心挪到0,0点
        canvas.drawBitmap(Util.getAvatar(BITMAP_WIDTH), MARGIN, MARGIN, paint);//1.先把图片画出来,注意坐标
        canvas.restore();

    }


    public static float getZForCamera() {
        return -6 * Resources.getSystem().getDisplayMetrics().density;
    }
}
